﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HSCardReaderNamespace;

namespace TestCardFormNameSpace
{
    public partial class TestCardForm : Form
    {
        public HSCardClass objHSCardApi = new HSCardClass();
        private int inFileSelected = 0;
        public string strResp;

        public TestCardForm()
        {
            InitializeComponent();
            cboxHistorical.Items.Add("00");
            cboxHistorical.Items.Add("01");
            cboxHistorical.Items.Add("02");
            cboxHistorical.Items.Add("03");
            cboxHistorical.Items.Add("04");
            cboxHistorical.Items.Add("05");
            cboxHistorical.Items.Add("06");
            cboxHistorical.Items.Add("07");
            cboxHistorical.Items.Add("08");
            cboxHistorical.Items.Add("09");
            cboxHistorical.Items.Add("0A");
            cboxHistorical.Items.Add("0B");
            cboxHistorical.Items.Add("0C");
            cboxHistorical.Items.Add("0D");
            cboxHistorical.Items.Add("0E");
            cboxHistorical.Items.Add("0F");
            cboxHistorical.Items.Add("FF");            
        }

       
        /// <summary>
        /// READ DATOS TARJETA      /////////////////////////////////////////////////////////////////////////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btReadCard_Click(object sender, EventArgs e)
        {            
                try
                {
                    inFileSelected= inGetFileSelectedRead();

                    if (inFileSelected != 0)
                    {
                        txboxReadCard.Text = objHSCardApi.LeerEnTarjeta(inFileSelected);
                        MessageBox.Show("DATOS LEIDOS CORRECTAMENTE", "ALERTA", MessageBoxButtons.OK);

                    }
                    else
                    {
                        MessageBox.Show("FILE SELECTOR ERROR - NO SE SELECCIONO NINGUN ARCHIVO PARA LEER EN LA TARJETA", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("NO SE PUDO LEER EN LECTORA LA TARJETA \n Exception: " + ex, "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                
         
        }

        public int inGetFileSelectedRead()
        {
            if (chboxReadFile1.Checked == true)
            {
                return 1;
            }
            else if (chboxReadFile2.Checked == true)
            {
                return 2;
            }
            else if (chboxReadFile3.Checked == true)
            {
                return 3;
            }
            else if (chboxReadFile4.Checked == true)
            {
                return 4;
            }
            else 
            {
                return 0;
            }

        }

        private void btCleanRead_Click(object sender, EventArgs e)
        {
            txboxReadCard.Clear();
        }

        /// <summary>
        /// WRITE DATOS EN TARJETA      /////////////////////////////////////////////////////////////////////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btWriteCard_Click(object sender, EventArgs e)
        {
            try
            {
                
                    inFileSelected= inGetFileSelectedWrite();
                    if (inFileSelected != 0)
                    {   
                        
                        if ((txboxWriteCard.Text != null) && (txboxWriteCard.Text != ""))
                        {
                            switch(inFileSelected)
                            {
                                case 1:
                                    if(txboxWriteCard.Text.Length<=objHSCardApi.inLongitudFile1)
                                    {   
                                        objHSCardApi.escribirenTarjeta(txboxWriteCard.Text, inGetFileSelectedWrite());
                                        MessageBox.Show("SE ESCRBIERON LOS DATOS CORRECTAMENTE", "ALERTA", MessageBoxButtons.OK);
                                    }
                                    else
                                    {
                                        MessageBox.Show("DATOS SUPERIORES A LA CANTIDAD PERMITIDA, MAXIMO: " + objHSCardApi.inLongitudFile1, "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                    break;
                                case 2:
                                    if (txboxWriteCard.Text.Length <= objHSCardApi.inLongitudFile2)
                                    {
                                        objHSCardApi.escribirenTarjeta(txboxWriteCard.Text, inGetFileSelectedWrite());
                                        MessageBox.Show("SE ESCRBIERON LOS DATOS CORRECTAMENTE", "ALERTA", MessageBoxButtons.OK);
                                    }
                                    else
                                    {
                                        MessageBox.Show("DATOS SUPERIORES A LA CANTIDAD PERMITIDA, MAXIMO: " + objHSCardApi.inLongitudFile2, "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                    break;
                                case 3:
                                    if (txboxWriteCard.Text.Length <= objHSCardApi.inLongitudFile3)
                                    {
                                        objHSCardApi.escribirenTarjeta(txboxWriteCard.Text, inGetFileSelectedWrite());
                                        MessageBox.Show("SE ESCRBIERON LOS DATOS CORRECTAMENTE", "ALERTA", MessageBoxButtons.OK);
                                    }
                                    else
                                    {
                                        MessageBox.Show("DATOS SUPERIORES A LA CANTIDAD PERMITIDA, MAXIMO: "+objHSCardApi.inLongitudFile3, "ALERTA", MessageBoxButtons.OK,MessageBoxIcon.Error);
                                    }
                                    break;
                                case 4:
                                    if (txboxWriteCard.Text.Length <= objHSCardApi.inLongitudFile4)
                                    {
                                        objHSCardApi.escribirenTarjeta(txboxWriteCard.Text, inGetFileSelectedWrite());
                                        MessageBox.Show("SE ESCRBIERON LOS DATOS CORRECTAMENTE", "ALERTA", MessageBoxButtons.OK);
                                    }
                                    else
                                    {
                                        MessageBox.Show("DATOS SUPERIORES A LA CANTIDAD PERMITIDA, MAXIMO: " + objHSCardApi.inLongitudFile4, "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                    break;
                                default:

                                    break;                                
                            }
                        }
                        else
                        {
                            MessageBox.Show("NO SE INGRESO NINGÚN DATO A GUARDAR EN LA TARJETA \n INGRESAR DATOS VÁLIDOS", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    else
                    {
                        MessageBox.Show("FILE SELECTOR ERROR - NO SE SELECCIONO NINGUN ARCHIVO FILE PARA ESCRIBIR EN LA TARJETA", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        
                        return;
                    }

            }
            catch (Exception ex)
            {
                MessageBox.Show("NO SE PUDO ESCRIBIR LA TARJETA \n Exception: " + ex, "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        public int inGetFileSelectedWrite()
        {
            if (chboxWriteFile1.Checked == true)
            {               
                return 1;
            }
            else if (chboxWriteFile2.Checked == true)
            {
                return 2;
            }
            else if (chboxWriteFile3.Checked == true)
            {
                return 3;
            }
            else if (chboxWriteFile4.Checked == true)
            {
                return 4;
            }
            else
            {
                return 0;
            }

        }

        private void btCleanWrite_Click(object sender, EventArgs e)
        {
            txboxWriteCard.Clear();
        }





        /// <summary>
        /// IDENTIFICAR TARJETA     ////////////////////////////////////////////////////////////////////////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btListarLectora_Click(object sender, EventArgs e)
        {
            try
            {
                lboxListarLect.Items.Clear();
               lboxListarLect.Items.Add(objHSCardApi.ListarLectorasCards());
            }catch(Exception ex)
            {
                MessageBox.Show("NO SE PUDO LISTAR LECTORA" + ex, "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
           
            }
        }
               

        /// <summary>
        /// GET ATR         ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btObtenerAtr_Click(object sender, EventArgs e)
        {

            try
            {
                txboxGetATR.Text = objHSCardApi.identificarTarjeta();
                txboxLengthATR.Text = txboxGetATR.Text.Length.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("NO SE PUDO IDENTIFICAR TARJETA" + ex, "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            
        }





        #region CHECK BOX FILES CHANGES
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// CHANGE OF CHECKBOXS     ///////////////////////////////////////////////////////////////////////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        ///////////////////////////////////////// READ CHECK BOX ////////////////////////
        private void chboxReadFile1_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxReadFile1.Checked == true)
            {
                chboxReadFile2.Checked = false;
                chboxReadFile3.Checked = false;
                chboxWriteFile4.Checked = false;
            }
            else
            {
                return;
            }
        }

        private void chboxReadFile2_CheckedChanged(object sender, EventArgs e)
        {

            if (chboxReadFile2.Checked == true)
            {
                chboxReadFile1.Checked = false;
                chboxReadFile3.Checked = false;
                chboxWriteFile4.Checked = false;
            }          
            else
            {
                return;
            }
        }

        private void chboxReadFile3_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxReadFile3.Checked == true)
            {
                chboxReadFile1.Checked = false;
                chboxReadFile2.Checked = false;
                chboxWriteFile4.Checked = false;
            }
            else
            {
                return;
            }
        }
        private void chboxReadFile4_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxReadFile4.Checked == true)
            {
                chboxReadFile1.Checked = false;
                chboxReadFile2.Checked = false;
                chboxWriteFile3.Checked = false;
            }
            else
            {
                return;
            }
        }

        //////////////////////////////WRITE CHECKBOX //////////////////////////////////////////////
        private void chboxWriteFile1_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxWriteFile1.Checked == true)
            {
                chboxWriteFile2.Checked = false;
                chboxWriteFile3.Checked = false;
                chboxWriteFile4.Checked = false;
            }
            else
            {
                return;
            }
        }

        private void chboxWriteFile2_CheckedChanged(object sender, EventArgs e)
        {

            if (chboxWriteFile2.Checked == true)
            {
                chboxWriteFile1.Checked = false;
                chboxWriteFile3.Checked = false;
                chboxWriteFile4.Checked = false;
            }
            else
            {
                return;
            }
        }

        private void chboxWriteFile3_CheckedChanged(object sender, EventArgs e)
        {

            if (chboxWriteFile3.Checked == true)
            {
                chboxWriteFile1.Checked = false;
                chboxWriteFile2.Checked = false;
                chboxWriteFile4.Checked = false;
            }
            else
            {
                return;
            }
        }
        private void chboxWriteFile4_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxWriteFile4.Checked == true)
            {
                chboxWriteFile1.Checked = false;
                chboxWriteFile2.Checked = false;
                chboxWriteFile3.Checked = false;
            }
            else
            {
                return;
            }
        }

        #endregion

        private void txboxWriteCard_TextChanged(object sender, EventArgs e)
        {
            tboxLongWrite.Clear();
            tboxLongWrite.Text = txboxWriteCard.Text.Length.ToString();

        }
        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///  INTERFAZ SENDER AND RECEIVER
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// ////////////////////////////////////////////////////////////////////////////////////////
        private void btSendCommand_Click(object sender, EventArgs e)
        {            

            if((txboxCLA.Text!=null)&&(txboxCLA.Text!="")&&(txboxINS.Text!=null)&&(txboxINS.Text!="")&&(txboxP1.Text!=null)&&(txboxP1.Text!="")&&(txboxP2.Text!=null)&&(txboxP2.Text!=""))
            {

                if (inValidacionINSComandoXLenght(txboxINS.Text,txboxDataSender.Text)>=0)
               {
                   
                   byte byCLA = objHSCardApi.HexStringToByte(txboxCLA.Text);

                   MessageBox.Show("CLA: " + string.Format("{0:X2}", byCLA), "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);


                   byte byINS = objHSCardApi.HexStringToByte(txboxINS.Text);

                   byte byP1 = objHSCardApi.HexStringToByte(txboxP1.Text) ;
                   byte byP2 = objHSCardApi.HexStringToByte(txboxP2.Text) ;
            
                    try
                    {
                        string strComandoRecibido = objHSCardApi.SendReceiveCommands(byCLA, byINS, byP1, byP2, txboxDataSender.Text);
                        if (strComandoRecibido != null)
                        {
                            txboxReceiberCMD.Text = strComandoRecibido;

                        }
                        else
                        {
                            MessageBox.Show("SE RECIBIO NULL DE RESPUESTA", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            voLimpiarCamposInterface();
                        }
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show("EXCEPTION AL ENVIAR: " + ex, "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        voLimpiarCamposInterface();
                    }
               }else
               {
                   MessageBox.Show("ERROR - NO COINCIDE COMANDO CON TAMAÑO DE DATOS A ENVIAR", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                   voLimpiarCamposInterface();
               }
           }else
           {
               MessageBox.Show("DATOS DE HEADER INCOMPLETOS, revisar CLA, INS,...","ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
               voLimpiarCamposInterface();
           }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            voLimpiarCamposInterface();
        }

        public void voLimpiarCamposInterface()
        {
            txboxCLA.Clear();
            txboxINS.Clear();
            txboxP1.Clear();
            txboxP2.Clear();
            txboxDataSender.Clear();
            txboxReceiberCMD.Clear();
        }
        public int inValidacionINSComandoXLenght(string strComando,string strTexto)
        {
            switch (strComando)
            { 
                case "20":
                    return 0;
                case "24":      //PIN
                    return 8;
               default:
                    break;
                   
                
            }

            return -1;
        }



        /// <summary>   //////////////////////////////////////////////////////////////////////////////////////////////////
        /// Checkbox Changes ///////////////////////////////////////////////////////////////////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param> //////////////////////////////////////////////////////////////////////////////////////
        #region Checkbox Functions Change
        private void chboxPinChange_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxPinChange.Checked == true)
            {
                chboxCreditAmount.Checked = false;
                chboxClearCard.Checked = false;
                chboxGetCardInfo.Checked = false;
                chboxDebitAcc.Checked = false;
                chboxSubmitCode.Checked = false;
                chboxSelectFile.Checked = false;
            }
        }

        private void chboxCreditAmount_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxCreditAmount.Checked == true)
            {
                chboxPinChange.Checked = false;
                chboxClearCard.Checked = false;
                chboxGetCardInfo.Checked = false;
                chboxDebitAcc.Checked = false;
                chboxSubmitCode.Checked = false;
                chboxSelectFile.Checked = false;
            }
        }

        private void chboxClearCard_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxClearCard.Checked == true)
            {
                chboxCreditAmount.Checked = false;
                chboxPinChange.Checked = false;
                chboxGetCardInfo.Checked = false;
                chboxDebitAcc.Checked = false;
                chboxSubmitCode.Checked = false;
                chboxSelectFile.Checked = false;
            }
        }

        private void chboxGetCardInfo_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxGetCardInfo.Checked == true)
            {
                chboxCreditAmount.Checked = false;
                chboxClearCard.Checked = false;
                chboxPinChange.Checked = false;
                chboxDebitAcc.Checked = false;
                chboxSubmitCode.Checked = false;
                chboxSelectFile.Checked = false;
            }
        }

        private void chboxDebitAcc_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxDebitAcc.Checked == true)
            {
                chboxCreditAmount.Checked = false;
                chboxClearCard.Checked = false;
                chboxGetCardInfo.Checked = false;
                chboxPinChange.Checked = false;
                chboxSubmitCode.Checked = false;
                chboxSelectFile.Checked = false;
            }
        }

        private void chboxSubmitCode_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxSubmitCode.Checked == true)
            {
                chboxCreditAmount.Checked = false;
                chboxClearCard.Checked = false;
                chboxGetCardInfo.Checked = false;
                chboxPinChange.Checked = false;
                chboxDebitAcc.Checked = false;
                chboxSelectFile.Checked = false;
            }

        }
        private void chboxSelectFile_CheckedChanged(object sender, EventArgs e)
        {
            if (chboxSelectFile.Checked == true)
            {
                chboxSubmitCode.Checked = false;
                chboxCreditAmount.Checked = false;
                chboxClearCard.Checked = false;
                chboxGetCardInfo.Checked = false;
                chboxPinChange.Checked = false;
                chboxDebitAcc.Checked = false;
            }

        }
        #endregion

        private void btFixSender_Click(object sender, EventArgs e)
        {
            if (chboxPinChange.Checked == true)
            {
                if((txboxPinChange.Text.Length == 8)&&(txboxPinCodeSubmit.Text.Length==8)&&(txboxCodeNoPin.Text.Length==2))
                {
                    try
                    {
                        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                        Byte[] byICCodeToSubmit = encoding.GetBytes(txboxPinCodeSubmit.Text);

                        byte byICCodeNoPin = objHSCardApi.HexStringToByte(txboxCodeNoPin.Text);

                        strResp = objHSCardApi.ContextoChangePin(txboxPinChange.Text, byICCodeToSubmit,byICCodeNoPin);

                        if (strResp != null)
                        {
                            txboxReceiberCMD.Text = strResp;
                            chboxPinChange.Checked = false;
                        }
                        else
                        {
                            MessageBox.Show("NO SE PUDO ENVIAR EL COMANDO DE PIN - NULL", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }catch(Exception Ex)
                    {
                        MessageBox.Show("ERROR EXCEPTION AL ENVIAR COMANDO PIN: "+Ex, "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else 
                {
                    MessageBox.Show("Faltan digitos del pin. Deben ser 8", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }
            else if(chboxClearCard.Checked==true)
            {

            }
            else if (chboxCreditAmount.Checked == true)
            {

            }
            else if (chboxDebitAcc.Checked == true)
            {

            }
            else if (chboxGetCardInfo.Checked == true)
            {
                if ((txboxP1GetCardInfo.Text.Length == 2) && (txboxP3GetCardInfo.Text.Length == 2))
                {
                    try
                    {
                        byte byP1GetCardInfo = objHSCardApi.HexStringToByte(txboxP1GetCardInfo.Text);
                        byte byP3GetCardInfo = objHSCardApi.HexStringToByte(txboxP3GetCardInfo.Text);
                        
                        strResp = objHSCardApi.ContextoGetCardInfo(byP1GetCardInfo, byP3GetCardInfo);

                        if (strResp != null)
                        {
                            txboxReceiberCMD.Text = strResp;
                            chboxGetCardInfo.Checked = false;
                        }
                        else
                        {
                            MessageBox.Show("NO SE PUDO ENVIAR EL COMANDO DE GET CARD INFO - NULL", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    catch (Exception Ex)
                    {
                        MessageBox.Show("ERROR EXCEPTION AL ENVIAR COMANDO GET CARD INFO: " + Ex, "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("Faltan digitos de P1 y P3 para Get Card Info", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }
            else if (chboxSubmitCode.Checked == true)
            {
               
                    if((txboxP1SubmitCode.Text.Length == 2)&&(txboxDataSubmitCode.Text.Length==8))
                    {
                        try
                        {
                            byte byP1Code=objHSCardApi.HexStringToByte(txboxP1SubmitCode.Text);
                            strResp = objHSCardApi.ContextoSubmitCodeToCard(txboxDataSubmitCode.Text, byP1Code);

                            if (strResp != null)
                            {
                                txboxReceiberCMD.Text = strResp;
                                chboxPinChange.Checked = false;
                            }
                            else
                            {
                                MessageBox.Show("NO SE PUDO ENVIAR EL COMANDO DE PIN - NULL", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }catch(Exception Ex)
                        {
                            MessageBox.Show("ERROR EXCEPTION AL ENVIAR COMANDO PIN: "+Ex, "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    else 
                    {
                        MessageBox.Show("Faltan digitos de P1 y DATA", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
            }
            else if (chboxSelectFile.Checked == true)           ///////// SELECT FILE /////////////
            {

                if((txboxSelectFile.Text.Length == 2)&&(txboxSelectFile2.Text.Length == 2))
                {
                    try
                    {
                        byte[] byID=new byte[]{objHSCardApi.HexStringToByte(txboxSelectFile.Text),objHSCardApi.HexStringToByte(txboxSelectFile2.Text)};

                        strResp = objHSCardApi.ContextoSelectFile(byID);
                       

                        if (strResp != null)
                        {
                            txboxReceiberCMD.Text = strResp;
                            chboxSelectFile.Checked = false;
                        }
                        else
                        {
                            MessageBox.Show("NO SE PUDO ENVIAR EL COMANDO SELECT FILE - NULL", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    catch (Exception Ex)
                    {
                        MessageBox.Show("ERROR EXCEPTION AL ENVIAR COMANDO SELECT FILE: " + Ex, "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("Faltan digitos de DATA File Selector", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            
            }
            else
            {
                MessageBox.Show("NO HAY NINGUN TIPO DE COMANDO SELECCIONADO", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
        }

        /// <summary>   /////////////////////////////////////////////////////////////////////////////////////////////////
        /// ////////////////////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void btClearFix_Click(object sender, EventArgs e)
        {

        }

        private void btClearReceiver_Click(object sender, EventArgs e)
        {
            txboxReceiberCMD.Clear();

        }

        /// <summary>   /////////////////////////////////////////////////////////////////////////////////////////////////
        /// UPDATE ATR NEW - NUEVO ATR
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void btUpdateATR_Click(object sender, EventArgs e)
        {
            if ((txboxATR.Text != null) && (txboxATR.Text != "") && (cboxHistorical.Text != null) && (cboxHistorical.Text != "") 
                && (txboxCodeNoATR.Text.Length==2)&&(txboxATRSubmitCode.Text.Length==8))
            {
                if (txboxATR.Text.Length > 10)
                {

                    
                    
                    System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                    
                    int x=0;
                    int y=0;
                    int inLen = txboxATR.Text.Length / 2;
                    byte[] byID = new byte[inLen];

                    while (x<inLen)
                    {
                        byID[x] = objHSCardApi.HexStringToByte(txboxATR.Text.Substring(y, 2));

                       // MessageBox.Show("ID" + x.ToString() + ": " + string.Format("{0:x2}", byID[x]).ToUpper(), "ALERTA", MessageBoxButtons.OK);
                        
                        y=y+2;
                        x++;
                    }
                    byte byCodeNo = objHSCardApi.HexStringToByte(txboxCodeNoATR.Text);

                    byte[] byATRICSubmitCode = encoding.GetBytes(txboxATRSubmitCode.Text);
                    //if (objHSCardApi.ContextoUpdateATR(byID, Convert.ToInt32(objHSCardApi.HexStringToByte(cboxHistorical.Text))) == true)
                    if(objHSCardApi.ContextoUpdateATR(byID, (objHSCardApi.HexStringToByte(cboxHistorical.Text)), byATRICSubmitCode , byCodeNo) == true)
                    {
                        MessageBox.Show("DATOS ATR COPIADOS CORRECTAMENTE", "ALERTA", MessageBoxButtons.OK);
        
                    }
                    else
                    {
                        MessageBox.Show("NO SE PUDO ENVIAR EL COMANDO UPDATE - NULL", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("ATR MENOR A 10 DIGITOS - COLOQUE ATR CORRECTO", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
             
                }
            }
            else
            {
                MessageBox.Show("NO HAY ANOTADO NINGUN ATR ó NO HAY HISTORICO, CODE IC, COMPLETAR DATOS CORRECTAMENTE", "ALERTA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
             
            }

        }

        /// <summary>
        /// //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click_1(object sender, EventArgs e)
        {
            txboxGetATR.Clear();
            txboxLengthATR.Clear();
        }


    }


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // COMANDOS INS /////////////////////////
    /*
     
        START SESSION 84 h
        AUTHENTICATE 82 h
        GET RESPONSE C0 h
        SUBMIT CODE  
     
     
      
    */
}
